<?php
	class photoAction extends userAction {
		
		private $feedSetting = false; //群组操作是否加入动态
		protected function _initialize() {
			parent::_initialize();
			$this->feedSetting = getAddFeedSetting($this->userInfo['id'],"picaction"); 
		}
		
		/**
		  *我好友的，或者某个好友，或者自己的相册列表
		  *
		  **/
		public function index(){
			import("ORG.Util.Page"); 
			$album 		= D("albums");
			isset($_GET["u"]) ? $u = safeRequest($_GET['u'],1) : $u= "all"; 
			isset($_GET["p"]) ? $p = $_GET["p"] : $p = 1;       
			if ("all"== $u){ 		
				$userId 	= 	$this->userInfo['id'];
				$friendIds 	= 	getFriendList($this->userInfo['id']);
				$albumCount = 	$album->count("userid in ($friendIds) " ); 
				$listRows 	= 	20; 
				$p 			= 	new Page($albumCount,$listRows); 
				$p			->	setConfig('header' ,L("photo_page_title")); 
				$albumList	=	$album->findAll("userid in ($friendIds)","*",'id desc',$p->firstRow.','.$p->listRows);
				$page  		= 	$p->show();
				$this		->	assign("albumCount",$albumCount);
				$this		->	assign("albumList",$albumList); 
				$this		->	assign("page",$page);
				$this		->	display(); 
			} else {
				$this->rank =  getRank($this->userInfo['id'],$u,"photo");
				if(!$this->rank){
					redirect(__APP__."/rank/norank/u/".$u);
					return false;
				}
				$albumCount = 	$album->count("userid = '$u' "); 
				$listRows 	= 	20; 
				$p 			= 	new Page($albumCount,$listRows); 
				$p			->	setConfig('header' ,L("photo_page_title")); 
				$albumList	=	$album->findAll("userid = '$u'","*",'id desc',$p->firstRow.','.$p->listRows);
				$page  		= $p->show();
				if( $u != $this->userInfo['id']){
					$userDao 	= 	D("user");
					$userInfo 	= 	$userDao->find($u);
					$this		->	assign("userInfo",$userInfo);					
				}
				$this		->	assign("myId",$this->userInfo['id']);
				$this		->	assign("albumCount",$albumCount); 
				$this		->	assign("albumList",$albumList); 
				$this		->	assign("page",$page); 
				$this		->	display("my");
				
			}
		}
		
		
		/**
		  * 某个相册的相片列表
		  *
		  *
		  */
		public function showalbum(){
			import("ORG.Util.Page"); 
			isset($_GET["p"]) ? $p = $_GET["p"] : $p = 1;
			$albumid 	= 	safeRequest($_GET["id"]);
			$album = D("albums")->find("id=".$albumid,"userid,name,photonum");
			$dao 		= 	D("albums_photos");
			$userid 	= 	$album['userid'];
			$listRows   =   C("publicListRows"); //每页显示20个相册
			$p = new Page($album['photonum'],$listRows); //实例化分页类
			$p->setConfig('header' ,L("photo_page_title")); 
			$list = $dao->findAll("userid=$userid and albumid=$albumid","*",'id desc',$p->firstRow.','.$p->listRows);
			$page  = $p->show();
			if( $userid != $this->userInfo['id']){
				$userDao = D("user");
				$userInfo = $userDao->find($userid );
				$this->assign("userInfo",$userInfo);
			}
			
			$this->assign("album",$album);
			$this->assign("myId",$this->userInfo['id']);
			$this->assign("photolist",$list);
			$this->assign("page",$page); //绑定分页
			$this->display();
		}
		
		
		/**
		  * 创建相册
		  *
		  *
		  **/
		public function createalbum(){
			$photoSetting = getAddFeedSetting($this->userInfo['id'],"photo",1); 
			$this->assign("photosetting",$photoSetting);
			$this->display();
		}
		
		
		/**
		  * 保存创建的相册
		  *
		  *
		  **/		
		public function savealbum(){
			if("" == trim($_POST["albumname"])){
				$this->error(L("no_albumname"));
				return false;
			}
			$dao = D("albums");
			$dao->userid = $this->userInfo['id'];
			$dao->name = str_filter(trim($_POST["albumname"]));
			$dao->desc = str_filter(trim($_POST["albumdesc"]));
			$dao->regtime = time();
			$dao->modifytime = time();
			$dao->setting = str_filter($_POST["privacy"]);
			$id = $dao->add();
			if($id>0){
				makeAlbum($this->userInfo['id'],$id);
				D("cache")->setField("albumCount","(albumCount+1)","userid=".$this->userInfo['id']);
				$this->redirect("uploadphoto",MODULE_NAME,'',APP_NAME,array('albumid'=>$id));
			}else{
				$this->error(L("create_error"));
			}
		}

		/**
		  * 编辑相册
		  *
		  *
		  **/
		public function  editalbum(){
			$id = safeRequest($_GET["id"]);
			$dao = D("albums");
			$album = $dao->find($id);
			$this->assign("album",$album);
			$this->display();
		}

		/**
		  * 保存编辑相册
		  *
		  *
		  **/
		public function updatealbum(){
			$dao = D("albums");
			$dao->find(safeRequest($_POST["id"]));
			$dao->name = str_filter(trim($_POST["albumname"]));
			$dao->desc = str_filter(trim($_POST["albumdesc"]));
			$dao->setting = str_filter($_POST["privacy"]);
			if($dao->save()){
				isset($_POST["ajax"]) ? print "success" : $this->success(L("edit_album_success"));

			}else{
				isset($_POST["ajax"]) ? print "error" : $this->error(L("edit_album_error"));
			}
		}
		
		/**
		  * 上传相片
		  *
		  **/
		public function uploadphoto(){
			$dao = D("albums");
			$albumlist = $dao->findAll("userid=".$this->userInfo['id']);
			if(empty($albumlist)){
				$this->redirect("createalbum");
			}else{
				$this->assign("albumlist",$albumlist);
				$this->display();
			}
		}
		
		
		/**
		  * 保存上传的图片
		  *
		  *
		  *
		  **/
		public function savephoto(){
			$albumInfo = explode(",",safeRequest($_POST["album"],1));
			import("ORG.Net.UploadFile");
        	$upload = new UploadFile();
        	$upload->allowExts = array("jpg","gif","png");
        	$path = getDeep((int)$albumInfo[0])."/";
        	$upload->savePath = "./Public/Uploads/photo/".$path;
        	$upload->fileExtension = true; 
        	$upload->uploadReplace = true; //重名替换
        	$upload->thumb = true;
        	$upload->thumbMaxWidth = "600,126";
        	$upload->thumbMaxHeight = "450,96";
        	$upload->saveRule = "uniqid";
        	$upload->thumbSuffix = "./Public/Uploads/photo/big/".$path."/,./Public/Uploads/photo/small/".$path."/";
        	if($upload->upload()){
        		$img = $upload->getUploadFileInfo();
				$count = count($img);
        		$dao = D("albums_photos");
				for($i=0;$i<$count;$i++){
					$data[$i]["albumid"]    =  $albumInfo[0];
					$data[$i]["userid"]     =  $this->userInfo['id'];
					$data[$i]["photoname"]  =  $path.$img[$i]["savename"];
					$data[$i]["regtime"]    =  time();
					$data[$i]["setting"]    =  intval($_POST["privacy"]);
				}
				$id = $dao->addAll($data);
        		if($id){
					$albumDao = D("albums");
					$albumDao->find($albumInfo[0]);
					$albumDao->photonum     =  '(photonum+'.$count.')';
					$albumDao->modifytime   =  time();
					$albumDao->defaultphoto =  $path.$img[0]["savename"];
					$albumDao->save();
					if($this->feedSetting){
						addFeed($this->userInfo['id'],"addphoto",$id,$albumInfo[0],0,$count);
					}
					$this->redirect('editphoto','photo','',APP_NAME,array('id'=>$id,'count'=>$count,'albumnum'=>$albumInfo[2]+$count,'albumid'=>$albumInfo[0],'albumname'=>urlencode($albumInfo[1])),0);
        		}
        	}else{
        		$this->error($upload->error);
        	}
		}
		
		
		/**
		  * 被圈的相片
		  *
		  *
		  **/
		public function showtag(){ 
			import("ORG.Util.Page"); 
			isset($_GET["p"]) ? $p = $_GET["p"] : $p = 1;
			$u 	= 	safeRequest($_GET["u"]);
			$listRows = C("publicListRows"); 
			$dao = D("albums_photos_tags");
			$tagcount = $dao->count("taguserid=".$u,"id");
			$p = new Page($tagcount,$listRows); 
			$p->setConfig('header' ,L("photo_page_title")); 
			$list = $dao->findAll("taguserid=".$u,"*",'regtime desc',$p->firstRow.','.$p->listRows);
			$page  = $p->show();
			if( $u != $this->userInfo['id']){
				$userDao = D("user");
				$userInfo = $userDao->find("id=".$u,"id,username");
				$this->assign("userInfo",$userInfo);
			}
			$this->assign("photolist",$list);
			$this->assign("page",$page); 
			$this->display();
		}

		
		/**
		  * 设置相册封面
		  *
		  **/
		public function setdefault(){
			$albumid = safeRequest($_GET["albumid"]);
			if($this->setdefaultphoto($albumid , base64_decode($_GET["img"]))){
				isset($_GET["ajax"]) ? print "success" : $this->success(L("action_success"));
			}else{
				isset($_GET["ajax"]) ? print "error" : $this->success(L("action_error"));
			}
		}
		
		/**
		  * 保存封面设置
		  *
		  *
		  **/
		public function setdefaultphoto($albumid,$img){
			$albumDao = D("albums");
			return $albumDao->setField("defaultphoto",$img,"id=".$albumid." and userid=".$this->userInfo['id']);
		}
		
		
		/**
		  * 查看单张图片
		  *
		  *
		  **/
		public function showphoto(){
			$id = safeRequest($_GET["id"]);
			$dao = D("albums_photos");
			$photo = $dao->find("id=".$id);
			if($photo){
				$nextid = $dao->first("id<".$id." and albumid=".$photo['albumid'],"id desc","id"); //后一张
				$pervid = $dao->find("id>".$id." and albumid=".$photo['albumid'],"id","regtime desc","1"); //前一张
				if(!is_array($nextid)){ //以达最后一张，取第一张
					$nextid = $dao->last('albumid='.$photo['albumid'],"regtime","id"); 
				}
				if(!is_array($pervid)){ //以达第一张，则取最后一张
					$pervid = $dao->first('albumid='.$photo['albumid'],"regtime","id");
				}
				if( $photo['userid'] != $this->userInfo['id']){
					$userInfo   =  F("userInfo","",-1,getCachePath(APP_PATH."/UserCache",$this->userInfo['id']));
					$this->assign("userInfo",$userInfo);
				}
				$revertDao = D("albums_photos_reverts");
				$revertlist = $revertDao->findAll("photoid=".$id,"*","regtime asc");
				$album = D("albums")->find("id=".$photo['albumid'],"name,photonum,defaultphoto");
				$taglist = D("albums_photos_tags")->findAll("photoid=".$id);
				$this->assign("album",$album);
				$this->assign("myId",$this->userInfo['id']);
				$this->assign("photo",$photo);
				$this->assign("_nextid",$nextid['id']);
				$this->assign("_pervid",$pervid['id']);
				$this->assign("revertlist",$revertlist);
				$this->assign("taglist",$taglist);
				$this->display();
			}else{
				$this->error(L("no_record"));
			}
		}
		
		/**
		  * 编辑相片信息
		  * 
		  *
		  *
		  **/
		public function editphoto(){
			$id = safeRequest($_GET["id"]);
			$count = safeRequest($_GET["count"]);
			$dao = D("albums_photos");
			if($count == 1){
				$list = $dao->findAll("id=".$id." and userid=".$this->userInfo['id']);
			}else{
				$list = $dao->findAll("id>=".$id." and userid=".$this->userInfo['id'], '*','id desc');
			}
			if($list){
				$this->assign("list",$list);
				$this->display();
			}else{
			}
		}
        
		
		/**
		  * 保存编辑的相片信息
		  *
		  *
		  **/
		public function updatephoto(){
			$idlist = $_POST["id"];
			$count = count($idlist);
			$desclist = $_POST["photodesc"];
			$dao = D("albums_photos");
			for( $i=0; $i< $count ; $i++ ){
				$data['id'] = safeRequest($idlist[$i]);
				$data['photodesc'] = str_filter($desclist[$i]);
				$dao->save($data);
			}
			$albumid = safeRequest($_POST["albumid"]);
			if(isset($_POST["default"])){
				$this->setDefaultphoto($albumid,str_filter($_POST["default"]));
			}
			$this->redirect('showphoto','photo','',APP_NAME,array('id'=>$data["id"]),0);
			//$this->success(L("edit_photo_success"));
		}
 		
		
		/**
		  * 删除某张相片
		  *
		  *
		  **/
		public function deletephoto(){
			$id = safeRequest($_GET["id"]);
			$dao = D("albums_photos");
			$record = $dao->find("id=".$id." and userid=".$this->userInfo['id'],"userid,albumid,photoname");
			if($record){
				if($dao->delete("id=".$id)){
					D("albums")->setField("photonum","(photonum-1)","id=".$record['albumid']);//相片数减一
					D("albums_photos_reverts")->delete("photoid=".$id);//删除相关评论
					D("albums_photos_tags")->delete("photoid=".$id);//删除相关tag
					D("albums_photos_tags_request")->delete("photoid=".$id);//删除相关tag评论
					$path = getDeep($record['albumid'])."/";
					unlink("./Public/Uploads/photo/".$path."/".$record['photoname']); //删除原图
					unlink("./Public/Uploads/photo/big/".$path."/".$record['photoname']); //删除大图
					unlink("./Public/Uploads/photo/small/".$path."/".$record['photoname']); //删除小图
					isset($_GET["ajax"]) ? print "success" : $this->redirect('showalbum','photo','',APP_NAME,array('u'=>$record['userid'],'id'=>$record['albumid']),0);
				}else{
					isset($_GET["ajax"]) ? print "error" : $this->error(L("delete_photo_error"));
				}
			}else{
				isset($_GET["ajax"]) ? print "error" : $this->error(L("no_record"));
			}
		}
		
		/**
		  * 保存图片tag
		  *
		  *
		  **/
		public function savetag(){
			$photoid = safeRequest($_GET["pid"]);
			$taguserid = safeRequest($_GET["taguserid"]);
			$record = D("albums_photos")->find("id=".$photoid,"albumid,userid,photoname");
			if($record){
				if($record['userid'] == $this->userInfo['id']){ //自己圈人
					$dao = new albums_photos_tagsModel();
					$dao->userid = $record['userid'];
					$dao->albumid = $record['albumid'];
					$dao->photoid = $photoid;
					$dao->taguserid = $taguserid;
					$dao->tagcoordinate = intval($_GET["x"]).",".intval($_GET["y"]);
					$dao->requestid = $record['userid'];
					$dao->regtime = time();
					$dao->photoname = $record['photoname'];
					$id = $dao->add();
					if($id>0){
						if($this->feedSetting){
							addFeed($this->userInfo['id'],"phototag","在照片中圈中了",$photoid,$taguserid);//添加动态
						}
						if($taguserid != $this->userInfo['id']){
							addNotify($taguserid,$this->userInfo['id'],"phototag",$photoid,$id); //给圈中的人通知
						}
						isset($_GET["ajax"]) ? print "success" : $this->success(L("photo_tag_success"));
					}else{
						isset($_GET["ajax"]) ? print "error" : $this->error(L("photo_tag_error"));
					}
				}else{ //别人的圈人请求
					$dao = new albums_photos_tags_requestModel();
					$dao->userid = $record['userid'];
					$dao->albumid = $record['albumid'];
					$dao->photoid = $photoid;
					$dao->requestid = $this->userInfo['id'];
					$dao->taguserid = $taguserid;
					$dao->tagcoordinate = intval($_GET["x"]).",".intval($_GET["y"]);
					$dao->photoname = $record['photoname'];
					$id = $dao->add();
					if($id>0){
						isset($_GET["ajax"]) ? print "requestsuccess" : $this->success(L("photo_tag_request_success"));
					}else{
						isset($_GET["ajax"]) ? print "error" : $this->error(L("photo_tag_request_error"));
					}
				}
			}else{
				isset($_GET["ajax"]) ? print "error" : $this->error(L("no_record"));
			}
		}
		
		
		/**
		  * 确认圈人请求
		  *
		  *
		  **/
		public function checktag(){//确认圈人
			$id = safeRequest($_GET["id"]);
			$dao = new albums_photos_tags_requestModel();
			$record = $dao->find("id=".$id);
			if($record){
				if($record['userid'] == $this->userInfo['id']){ //是我的相片
					$tdao = new albums_photos_tagsModel();
					$tdao->userid = $record['userid'];
					$tdao->albumid = $record['albumid'];
					$tdao->photoid = $record['photoid'];
					$tdao->taguserid = $record['taguserid'];
					$tdao->tagcoordinate = $record['tagcoordinate'];
					$tdao->requestid = $record['requestid'];
					$tdao->photoname = $record['photoname'];
					$tao->regtime = time();
					$tid = $tdao->add();
					if($tid>0){
						addNotify($record['taguserid'],$record['requestid'],"phototag",$record['photoid'],$tid);
						addNotify($record['requestid'],$this->userInfo['id'],"checkphototag",$record['photoid'],$tid);
						$dao->delete("id=".$id); //删除圈人请求
						if($this->feedSetting){
							addFeed($record->requestid,"phototag","在照片中圈中了",$record->photoid,$record->taguserid);//添加动态
						}
						isset($_GET["ajax"]) ? print "success" : $this->success(L("check_photo_tag_success"));
					}else{
						isset($_GET["ajax"]) ? print "error" : $this->error(L("check_photo_tag_error"));
					}
				}else{
					isset($_GET["ajax"]) ? print "error" : $this->error(L("no_rank"));
				}
			}else{
				isset($_GET["ajax"]) ? print "error" : $this->error(L("no_record"));
			}
		}
		
		
		/**
		  * 忽略photo tag
		  *
		  *
		  **/
		public function ignoretag(){
			$id = safeRequest($_GET["id"]);
			if(D("albums_photos_tags_request")->delete("id=".$id." and userid=".$this->userInfo['id'])){
				isset($_GET["ajax"]) ? print "success" : $this->success(L("ignoretag_success"));
			}else{
				isset($_GET["ajax"]) ? print "error" : $this->error(L("ignoretag_error"));
			}
		}
		
		
		/**
		  * 删除相片tag
		  *
		  *
		  **/
		public function deltag(){
			$id = safeRequest($_GET["id"]);
			$dao = new albums_photos_tagsModel();
			$record = $dao->find("id=".$id,"userid,photoid,taguserid,requestid");
			if($record){
				$u = $this->userInfo['id'];
				if($u == $record['userid'] || $u == $record['raguserid'] || $u == $record['requestid']){ //相片所有者，圈人者，被圈者
					if($dao->delete("id=".$id)){
						isset($_GET["ajax"]) ? print "success" : $this->error(L("delete_phototag_success"));
					}else{
						isset($_GET["ajax"]) ? print "error" : $this->error(L("delete_phototag_error"));
					}
					delFeed(0,$record['requestid'],"phototag",$record['photoid'],$record['taguserid']);//删除相关动态
				}else{
					isset($_GET["ajax"]) ? print "error" : $this->error(L("no_rank"));
				}
			}else{
				isset($_GET["ajax"]) ? print "error" : $this->error(L("no_record"));
			}
		}
		
		
		/**
		  * 保存相片评论
		  *
		  *
		  **/
		public function savephotorevert(){
			$photoid = safeRequest($_POST["photoid"]);
			$reid = $_POST["reid"];
			$photo = D("albums_photos");
			$record = $photo->find("id=".$photoid,"albumid,userid");
			if($record){
				$revertDao = D("albums_photos_reverts");
				$albumid = $record['albumid'];
				$userid = $record['userid'];
				$content = str_filter($_POST["content"]);
				$revertDao->photoid = $photoid;
				$revertDao->albumid = $albumid;
				$revertDao->userid = $userid;
				$revertDao->revertsid = $this->userInfo['id'];
				$revertDao->postid = $reid;
				$revertDao->content = $content;
				$revertDao->regtime = time();
				$id = $revertDao->add();
				if($id > 0){
					$photo->setField("revertnum","(revertnum+1)","id=".$photoid);
					if($userid != $this->userInfo['id']){
						if($reid==0){
							addNotify($userid,$this->userInfo['id'],"photorevert",$photoid,$id);
						}else{
							addNotify($userid,$this->userInfo['id'],"photorevert2",$photoid,$id);
						}
					}
					if( $reid > 0 && $reid != $userid && $reid != $this->userInfo['id']){
						addNotify(intval($_POST["reid"]),$this->userInfo['id'],"photorevert2",$photoid,$id);
					}
					if($this->feedSetting){
						addFeed($this->userInfo['id'],"photorevert",$content,$photoid,$id);
					}
					isset($_POST["ajax"]) ? print $id.",".$this->userInfo['id'].",".$this->userInfo['username'] : $this->success(L("photo_revert_success"));
				}else{
					isset($_POST["ajax"]) ? print "error" : $this->error(L("photo_revert_error"));
				}
			}else{
				isset($_POST["ajax"]) ?  print "error" : $this->error(L("no_record"));
			}
		}

		
		/**
		  * 删除评论
		  * 
		  *
		  *
		  **/
		public function delphotorevert(){
			$revertid = safeRequest($_POST["revertid"]);
			$dao = D("albums_photos_reverts");
			$record = $dao->find("id=".$revertid,"photoid,userid,revertsid,postid");
			if($record){
				if($record['userid'] == $this->userInfo['id'] || $record['revertsid'] == $this->userInfo['id']){
					$dao->delete("id=".$revertid); 
					D("albums_photos")->setField("revertnum","(revertnum-1)","id=".$record['photoid']);
					if($record['postid'] == 0){
						delFeed(0,$this->userInfo['id'],"photorevert",0,$revertid);
					}else{
						delFeed(0,$this->userInfo['id'],"photorevert2",0,$revertid);
					}
					isset($_POST["ajax"]) ? print "success" : $this->success(L("delete_photo_revert_success"));
				}else {
					isset($_POST["ajax"]) ? print "error" : $this->error(L("delete_photo_revert_error"));
				}
			}else{
				isset($_POST["ajax"]) ?  print "error" : $this->error(L("no_record"));
			}
			
		}
	}
		
?>